﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;

namespace smartphone.App_Code
{
    public class Comment
    {
        /// <summary>
        ///Lay comment theo noi dung chua comment do
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public DataTable GetComments(int productID)
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "GetComments";

            DbParameter param = com.CreateParameter();
            param.ParameterName = "@maNoiDung";
            param.Value = productID;
            param.DbType = DbType.Int32;
            com.Parameters.Add(param);

            DataTable table = GenericDataAccess.ExecuteSelectCommand(com);
            return table;
        }

        /// <summary>
        /// Lay toan bo comment cho admin
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllComment()
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "GetAllComments";

            DataTable table = GenericDataAccess.ExecuteSelectCommand(com);
            return table;
        }

        /// <summary>
        /// Admin xoa comment
        /// </summary>
        /// <param name="commnetID"></param>
        /// <returns></returns>
        public bool DeleteComment(int commnetID)
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "DeleteComment";

            DbParameter param = com.CreateParameter();
            param.ParameterName = "@maBinhLuan";
            param.Value = commnetID;
            param.DbType = DbType.Int32;
            com.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(com);

            return (result != -1);
        }

        /// <summary>
        /// Tao commemnt
        /// </summary>
        /// <param name="email">Email nguoi post comment</param>
        /// <param name="name">Ten nguoi gui</param>
        /// <param name="content">Noi dung comment</param>
        /// <param name="contentID">Comment cua noi dung gi (san pham gi, tin tuc gi)</param>
        /// <returns></returns>
        public bool CreateComment(string email, string name, string content, int contentID)
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "CreateComment";

            DbParameter param = com.CreateParameter();
            param.ParameterName = "@email";
            param.Value = email;
            param.DbType = DbType.String;
            com.Parameters.Add(param);

            param = com.CreateParameter();
            param.ParameterName = "@hoTen";
            param.Value = name;
            param.DbType = DbType.String;
            com.Parameters.Add(param);

            param = com.CreateParameter();
            param.ParameterName = "@noiDung";
            param.Value = content;
            param.DbType = DbType.String;
            com.Parameters.Add(param);


            //ngay post comment
            string t = DateTime.Now.ToLongTimeString();
            param = com.CreateParameter();
            param.ParameterName = "@ngayPost";
            param.Value = t;
            param.DbType = DbType.DateTime;
            com.Parameters.Add(param);

            param = com.CreateParameter();
            param.ParameterName = "@maNoiDung";
            param.Value = contentID;
            param.DbType = DbType.Int32;
            com.Parameters.Add(param);


            int result = GenericDataAccess.ExecuteNonQuery(com);

            return (result != -1);
        }
    }
}